package com.jiangyao.interceptor;

import com.jiangyao.common.interceptor.BaseInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * 拦截器类配置
 *
 * @author: yangyongchuan
 * @create: 2020年04月17日 10:22
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

	/**
	 * 不需要验证登录用户操作权限的请求
	 */
	private final static List<String> EXCLUDE_PATH_PATTERNS;

	static {
		EXCLUDE_PATH_PATTERNS = new ArrayList<>();
		EXCLUDE_PATH_PATTERNS.add("/common/**");
		EXCLUDE_PATH_PATTERNS.add("/adminUser/login");
		EXCLUDE_PATH_PATTERNS.add("/error/**");
		EXCLUDE_PATH_PATTERNS.add("/demo/**");
		// swagger
		EXCLUDE_PATH_PATTERNS.add("/doc.html");
		EXCLUDE_PATH_PATTERNS.add("/webjars/**");
		EXCLUDE_PATH_PATTERNS.add("/v2/**");
		EXCLUDE_PATH_PATTERNS.add("/swagger-resources/**");

		EXCLUDE_PATH_PATTERNS.add("/swagger-ui.html");
		EXCLUDE_PATH_PATTERNS.add("/swagger-ui/*");
		EXCLUDE_PATH_PATTERNS.add("/swagger-resources/**");
		EXCLUDE_PATH_PATTERNS.add("/v2/api-docs");
		EXCLUDE_PATH_PATTERNS.add("/v3/api-docs");
		EXCLUDE_PATH_PATTERNS.add("/webjars/**");

	}

	/**
	 * interceptor配置
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {

		// 基础拦截器
		registry.addInterceptor(new BaseInterceptor()).addPathPatterns("/**");

		// 登录拦截器
		registry.addInterceptor(new LoginInterceptor())
				//添加需要验证登录用户操作权限的请求
				.addPathPatterns("/**")
				//这里add为“/**”,下面的exclude才起作用，且不管controller层是否有匹配客户端请求，拦截器都起作用拦截
				//排除不需要验证登录用户操作权限的请求
				.excludePathPatterns(EXCLUDE_PATH_PATTERNS);
	}

}
